#include "snake.h"
#include "ui_snake.h"
#include <Qpainter>
#include <QTimer>
#include <QKeyEvent>


Snake::Snake(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Snake),
    isRun(false)
{
    ui->setupUi(this);

    this->setGeometry(QRect(100,100,500,850));//界面
}

Snake::~Snake()
{
    delete ui;
}

void Snake::paintEvent(QPaintEvent *event){
    QPainter painter(this);

    painter.setPen(Qt::black);//设置画笔颜色
        painter.setBrush(Qt::red);//设置填充颜色
        painter.drawRect(20,20,460,730);
        //内墙
        painter.setPen(Qt::black);//设置画笔颜色
        painter.setBrush(Qt::black);//设置填充颜色
        painter.drawRect(30,30,440,710);
        QFont font2("Courier",15);
           painter.setFont(font2);
           painter.setPen(Qt::blue);
           painter.setBrush(Qt::blue);
           painter.drawText(40,800,scoreLabel);//显示得分
           painter.drawText(130,800,QString::number(nScore));


           painter.setPen(Qt::black);//设置画笔颜色
           painter.setBrush(Qt::yellow);//设置填充颜色
           painter.drawPixmap(food, QPixmap(":/shengming.jpeg"));


           QFont font3("Courier",15);
              painter.setFont(font3);
              painter.setPen(Qt::blue);
              painter.setBrush(Qt::blue);
              painter.drawText(240,800,lifeLabel);//显示得分
              painter.drawText(330,800,QString::number(life));




if(!isRun){
    InitSnake();
}

createRect();

if(qRects[0][0].top() > 730){
    qRects.dequeue();
}

//画块
painter.setPen(Qt::gray);
painter.setBrush(Qt::gray);

for (int i=0; i<qRects.size(); i++) {
    for (int j=0; j<density; j++) {
    painter.drawPixmap(qRects[i][j], QPixmap(":/huoqiou.jpeg"));
    }
}

//画人
painter.setPen(Qt::green);
painter.setBrush(Qt::green);
painter.drawPixmap(QRect(personX,personY,10,10), QPixmap(":/maliao.jpeg"));

timer->stop();
if(frameSpeed>40){
    frameSpeed--;
}
timer->start(frameSpeed);
 nScore+=1;
 nScorex+=1;

if (isCrash()){
    timer->stop();
}

}


void Snake::InitSnake(){
    isRun=true;
    scoreLabel="得分：";
    lifeLabel="生命：";
        nScore=0;
         food=Createect();


    qRects.clear();
    for(int i=0;i<40;i++){
        QVector<QRect> vRect;
        vRect.resize(density);
        for(int j=0;j<density;j++){
            int position=qrand()%44;
            vRect[j]=QRect(30+10*position,430-10*i,10,10);
        }
        qRects.enqueue(vRect);
    }



    timer=new QTimer(this);//设定定时器
    timer->start(frameSpeed);//设定帧速
    connect(timer,SIGNAL(timeout()),SLOT(Snake_update()));

}



void Snake::Snake_update(){

    rectsDown();
    switch(nPosition)
        {
        case 0:
        personY-=10;
            break;
        case 1:
        personX+=10;
            break;
        case 2:
        personY+=10;
            break;
        case 3:
        personX-=10;
            break;
        default:;
        }
    nPosition=-1;


    update();//更新

}



void Snake::keyPressEvent(QKeyEvent *event){
     QKeyEvent *key=(QKeyEvent*) event;
     switch(key->key())
   {
    case Qt::Key_W:nPosition=0;
      break;
    case Qt::Key_D:nPosition=1;
      break;
    case Qt::Key_S:nPosition=2;
      break;
    case Qt::Key_A:nPosition=3;
      break;
    default:;
   }
}


void Snake::createRect(){
    QVector<QRect> vRect;
    vRect.resize(density);
    for(int i=0;i<density;i++){
        int position=qrand()%44;
        vRect[i]=QRect(30+10*position,30,10,10);
    }
    qRects.enqueue(vRect);
}//随即队列

void Snake::rectsDown(){
    for (int i=0; i<qRects.size(); i++) {
        for (int j=0; j<density; j++) {
        qRects[i][j].setRect(qRects[i][j].left(),qRects[i][j].top()+10,10,10);
        }
    }
}


QRect Snake::Createect(){
    int x,y;
    x=qrand()%44;//生成小于25的整数
    y=qrand()%20;
    QRect rect(30+x*10,500+y*10,10,10);//食物小方块位置
    return rect;
}



bool Snake::isCrash(){
    if(personX>460||personX<30||personY<30||personY>730){
        return true;
       }

    for (int i=0; i<qRects.size(); i++) {
        for (int j=0; j<density; j++) {
        if(qRects[i][j].left()==personX&&qRects[i][j].top()==personY) {
            life--;
        }
        }
    }//碰撞判定
    if(personX==food.x()&&personY==food.y()){
        shi++;
        life++;
     food=Createect();

    }
    if(nScorex/500==1)
    {
      nScorex=0;
      life--;
      if(nScore>2000&&frameSpeed>20)
      {
          frameSpeed-=5;
      }

    }

    if(life<1)
    {
        return true;
    }
    return false;
}

